<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-11-04 23:54
 *
 * 项目：rm  -  $  - slidesType.php
 *
 * 作者：liwei 
 */

namespace modules\levfm\widgets\inputs\inputtypes;

use Lev;
use lev\helpers\SettingsHelper;
use lev\widgets\slides\slidesWidget;
use lev\widgets\slides\tablesnavWidget;
use lev\widgets\uploads\uploadsWidget;
use modules\levfm\widgets\inputs\inputsFormWidget;

!defined('INLEV') && exit('Access Denied LEV');


class slidesType extends tablesFormType
{

    public static function input($name, $value, $v)
    {

        return static::tablesData($v, 0, static::myInputs());
    }

    public static function formatViewData($input, $inputvalue) {
        $htm = '';

        $inputvalue = inputsFormWidget::tablesDataShowFormat($inputvalue, $input['moduleidentifier'], true);
        if ($inputvalue) {
            $inputset = uploadimgType::getSettings($input['settings']);
            $height = empty($inputset['height']) ? 130 : $inputset['height'];
            $htm .= '
<div class="slides-input- slides-input-' . $input['inputname'] . '">
    <div class="swiper-container slides-swiper">
        <div class="swiper-wrapper">';

            foreach ($inputvalue as $v) {
                if (!$v['status']) {
                    $img = '';
                    if ($v['logoupload']) {
                        if ($v['logoupload'][0] == '#') {
                            $img = '<svg class="icon c-'.$v['id'].'" aria-hidden="true"><use xlink:href="'.$v['logoupload'].'"></use></svg>';
                        }else {
                            $img = '<img alt="'.$v['name'].'" class="lazy c-' . $v['id'] . '" data-src="' . Lev::getAlias($v['logoupload']) . '">';
                        }
                    }
                    $v['_target'] = SettingsHelper::navTarget($v['target'], $v['link']);
                    if ($v['target'] != 99) {
                        $v['link'] = Lev::toRoute([$v['link']]);
                        $v['_link'] = $v['link'] ? '" href="' . $v['link'] : '';
                    } else {
                        $v['_link'] = '';
                    }

                    $v['ShowType'] && $img .= '<txt>'.$v['name'].'</txt>';
                    $htm .= '<div class="swiper-slide">
                                <a class="' . $v['_target'] . $v['_link'] . '">' . $img . '</a>
                            </div>';
                }
            }

            $htm .= '</div>
        <div class="swiper-pagination"></div>
    </div>
</div>';

            $style = '<style>.slides-input-'.$input['inputname'].' .swiper-slide {height: '.$height.'px;}</style>';
            $js = '
<script>jQuery(function() { window.setTimeout(function () { levfmJs.swiper("'.$input['inputname'].'"); }, 100); });</script>';

        }
        return $style.$htm.$js;
    }

    public static function myInputs()
    {
        return [
            'id' => array(
                'id' => 1,
                'order' => '1',
                'title' => 'ID',
                'inputname' => 'id',
                'inputtype' => 'number',
                'width' => '40',
                'settings' => '',
                'placeholder' => '必填，自动，保持默认即可'.static::inputIdPlaceholder(),
                'inputvalue' => '',
            ),
            'order' => array(
                'id' => 2,
                'order' => '2',
                'title' => '排序',
                'inputname' => 'order',
                'inputtype' => 'order',
                'width' => '40',
                'settings' => '',
                'placeholder' => '',
                'inputvalue' => '',
            ),
            'status' => array(
                'id' => 3,
                'order' => '3',
                'title' => '开关',
                'inputname' => 'status',
                'inputtype' => 'radio',
                'width' => '40',
                'settings' => '',
                'placeholder' => '',
                'inputvalue' => '',
            ),
            'ShowType' => array(
                'id' => 4,
                'order' => '4',
                'title' => '显示类型',
                'inputname' => 'ShowType',
                'inputtype' => 'select',
                'width' => '70',
                'settings' => '0=仅图片
2=图片和文字',
                'placeholder' => '',
                'inputvalue' => '',
            ),
            'target' => array(
                'id' => 5,
                'order' => '5',
                'title' => '打开方式',
                'inputname' => 'target',
                'inputtype' => 'selectcode',
                'width' => '80',
                'settings' => 'lev\\helpers\\SettingsHelper',
                'placeholder' => '',
                'inputvalue' => '',
            ),
            'name' => array(
                'id' => 6,
                'order' => '6',
                'title' => '图片名称或备注',
                'inputname' => 'name',
                'inputtype' => 'text',
                'width' => '',
                'settings' => '',
                'placeholder' => '',
                'inputvalue' => '',
            ),
            'link' => array(
                'id' => 7,
                'order' => '7',
                'title' => '链接地址',
                'inputname' => 'link',
                'inputtype' => 'text',
                'width' => '',
                'settings' => '',
                'placeholder' => '',
                'inputvalue' => '',
            ),
            'logoupload' => array(
                'id' => 8,
                'order' => '8',
                'title' => '幻灯图片',
                'inputname' => 'logoupload',
                'inputtype' => 'uploadimg',
                'width' => '',
                'settings' => '图片',
                'placeholder' => '#号开头调用icon图标、也可是@别名和url址址',
                'inputvalue' => '',
            ),
        ];
    }
}